package com.shy.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfiguration {

    @Value("${exchangeNotice}")
    private String exchangeNotice;
    @Value("${queueNotice}")
    private String queueNotice;

    /**
     * 创建自定义交换机
     *
     * @return
     */
    @Bean
    public CustomExchange exchangeNotice() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");

        return new CustomExchange(exchangeNotice, "x-delayed-message", true, false, args);
    }

    // 创建队列
    @Bean
    public Queue queueNotice() {
        return QueueBuilder.durable(queueNotice).build();
    }

    // 绑定队列到交换机
    @Bean
    public Binding bindingNotice(CustomExchange exchangeNotice, Queue queueNotice) {
        return BindingBuilder.bind(queueNotice).to(exchangeNotice).with("kq.notice").noargs();
    }

    //创建json转换器
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
